#-*- coding: utf-8 -*-

from CIMConnection import Client
from CIMConnection.CLILogParser import LSLogItemList
import threading
import sys

def init_globals():
   global dev
   global pool
   dev = None
   dsclient = Client.DSCLIClient()
   out = dsclient.dscli("lssi", LSLogItemList, 5*60)
   if out:
      dev = out[0].ID
      
#   out = dsclient.dscli("lsextpool -dev %s" % dev, LSLogItemList, 5*60)
#   if out:
#      maxsize = 500
#      for p in out:
#         if p.stgtype == 'fb' and int(p.available) > 500:
#            if int(p.available) > maxsize:
#               maxsize = int(p.available)
#               pool = p.ID
#   if not dev or not pool:
#      raise RuntimeError('Can not find a pool for testing')

def dscli_vol_create_delete(st, ed):
   dsclient = Client.DSCLIClient()
   cmd = "mkfbvol -dev %s -extpool %s -type ds -cap 1 -name autovol_#d %s-%s" % (dev, pool, st, ed)
   try:
      out = dsclient.dscli_without_wait(cmd)
   finally:
      cmd = "rmfbvol -quiet -dev %s %s-%s" % (dev, st, ed)
      out = dsclient.dscli_without_wait(cmd)

def dscli_vol_list():
   dsclient = Client.DSCLIClient()
   cmd = "lsfbvol -dev %s" % dev
   out = dsclient.dscli_without_wait(cmd)

def dscli_clean_up():
   print """"Input volume id prefix split with space.
   example: 0a 0c 0e
   : """
   startids = raw_input('')
   init_globals()
   idlist = startids.split()
   if not idlist:
      idlist = ['0a', '0c', '0e']
   plist = []
   def _clean(st, ed):
      dsclient = Client.DSCLIClient()
      cmd = "rmfbvol -quiet -dev %s %s-%s" % (dev, st, ed)
      out = dsclient.dscli_without_wait(cmd)
   for sid in idlist:
      st = sid + '00'
      ed = sid + 'ff'
      p = threading.Thread(target=_clean, args=(st, ed))
      plist.append(p)
   for p in plist:
      p.start()
   for p in plist:
      p.join()
   
def cim_probe():
   cimclient = Client.CIMClient()
   cimclient.connect_to_cimagent()
   cimclient.probe()
   
def main():
   print """"Input volume id prefix split with space.
   example: 0a 0c 0e
   : """
   startids = raw_input('')
   init_globals()   
   idlist = startids.split()
   if not idlist:
      idlist = ['0a', '0c', '0e']
   plist = []
   def _create_thread(st, ed):
      while True:
         dscli_vol_create_delete(st, ed)
         
   def _vollist_thread():
      while True:
         dscli_vol_list()
         
   def _probe():
      while True:
         cim_probe()
   
   for sid in idlist:
      st = sid + '00'
      ed = sid + 'ff'
      p = threading.Thread(target=_create_thread, args=(st, ed))
      plist.append(p)
   for i in range(2):
      p = threading.Thread(target=_vollist_thread, args=())
      plist.append(p)
      
   #plist.append(threading.Thread(target=_probe, args=()))
   
   for p in plist:
      p.start()
      
   for p in plist:
      p.join()
   

if __name__ == '__main__':
   print """
   wuweihui@cn.ibm.com
   use "ds8kcimlock.exe cleanup" to cleanup configurations
   """
   global pool
   if len(sys.argv) > 1:
      op = sys.argv[1]
      if op == "cleanup":
         dscli_clean_up()
         sys.exit(0)
   pool = raw_input('Input PoolID: ')
   main()
   